package com.klb.service.admin.impl;

import com.klb.dao.impl.AdminDaoImpl;
import com.klb.model.BbsAdmin;
import com.klb.service.admin.LoginServiceAdmin;
import com.klb.tools.CryptoUtils;
import com.klb.tools.Sessions;
import com.klb.tools.Tools;

/**
 * Created by fujia on 2016/7/2.
 */
public class LoginServiceAdminImpl implements LoginServiceAdmin {
    private AdminDaoImpl adminDao;

    public void setAdminDao(AdminDaoImpl adminDao) {
        this.adminDao = adminDao;
    }


    @Override
    public boolean loginIsSuccess(String username, String password) {
        /**
         * username不空且password不空
         * 创造bbsAdmin
         * bbsAdmin不空
         * 密码验证
         * 存放信息到session，返回结果
         * */
        if(!Tools.objectIsNullOrNot(username) && !Tools.objectIsNullOrNot(password)){
            BbsAdmin bbsAdmin = this.adminDao.getAdminByUsername(username);
            if (!Tools.objectIsNullOrNot(bbsAdmin)) {
                if (CryptoUtils.verify(bbsAdmin.getPassword(), password, bbsAdmin.getSalt())){
                    Sessions.setSession("adminId", bbsAdmin.getAdminId());
                    return true;
                }
            }
        }

        return false;
    }
}
